package com.gzz.orchid.main.facade.controller.example;

import com.gzz.orchid.common.webmvc.support.HttpResult;
import com.gzz.orchid.context.infrastructure.persistence.entity.sys.SysUserPo;
import com.gzz.orchid.context.infrastructure.persistence.mapper.sys.SysUserMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.random.RandomGenerator;

@Slf4j
@RestController
@RequestMapping("/example/mybatis-flex")
@RequiredArgsConstructor
public class MyBatisFlexController {
    private final SysUserMapper sysUserMapper;

    /**
     * @return
     */
    @GetMapping("/insert")
    public String testInsert() {
        SysUserPo po = new SysUserPo();
        // SysUserPo po = SysUserPo.createPojo();
        // Xoshiro256PlusPlus, LXM, SecureRandom,ChaCha20
        RandomGenerator rg = RandomGenerator.of("SecureRandom");

        po.setPasswd(String.valueOf(rg.nextGaussian()));
        po.setUserName("gzz");
        po.setPetName("gzz");
        po.setAllowIpaddr("192.168.1.1");
        po.setDenyIpaddr("192.168.1.1");
        po.setRoleCode("ADMIN");
        po.setSalt("salt");
        //sysUserMapper.insert(po);
        return "success";
    }

    /**
     * @return
     */
    @GetMapping("/select")
    public HttpResult testSelect() {
        SysUserPo sysUserPo = null;//sysUserMapper.selectOneById(101L);
        return HttpResult.success().data(sysUserPo);
    }

    /**
     * @return
     */
    @GetMapping("/update")
    public HttpResult testUpdate() {
//        SysUserPo po = sysUserMapper.selectOneById(101L);
//        po.setUserName("更新的");
//        sysUserMapper.update(po);
        return HttpResult.success();
    }

    /**
     * @return
     */
    @GetMapping("/delete")
    public HttpResult testDelete() {

//        QueryWrapper query = QueryWrapper.create()
//                .select()
//                .from(SYS_USER_PO.as("a"))
//                .where(SYS_USER_PO.ID.eq(1L));
//        sysUserMapper.deleteByQuery(query);
        return HttpResult.success();
    }

    /**
     * @return
     */
    @GetMapping("/selectById")
    public HttpResult testSelectById() {
        return HttpResult.success();
    }

    /**
     * @return
     */
    @GetMapping("/selectByCondition")
    public HttpResult testSelectByCondition() {
        return HttpResult.success();
    }

    /**
     * @return
     */
    @GetMapping("/selectByPage")
    public HttpResult testSelectByPage() {
        return HttpResult.success();
    }

    /**
     * @return
     */
    @GetMapping("/query")
    public HttpResult testQuery() {
//        QueryWrapper query = QueryWrapper.create()
//                .select()
//                .from(SYS_USER_PO.as("a"))
//                .where(SYS_USER_PO.USER_NAME.eq("admin"));
//        sysUserMapper.selectOneByQuery(query);
        return HttpResult.success();
    }

    /**
     * @return
     */
    @GetMapping("/db-insert")
    public HttpResult testDbInsert() {
        //使用原生 SQL 插入数据
        //        String sql = "insert into tb_account(id,name) value (?, ?)";
        //        Db.insertBySql(sql,1,"michael");
        //使用 Row 插入数据
//        Row account = new Row();
//        account.set("id", 100);
//        account.set(SYS_USER_PO.USER_NAME, "Michael");
//        Db.insert("sys_user", account);
        return HttpResult.success();
    }

    /**
     * @return
     */
    @GetMapping("/db-query")
    public HttpResult testDbQuery() {
        //使用原生 SQL 查询数据
        //查询所有大于 18 岁的用户
//        String listsql = "select * from tb_account where age > ?";
//        List<Row> rows = Db.selectListBySql(listsql, 18);
        return HttpResult.success();
    }
}
